<?php

namespace App\service;

use App\model\Mail;
use App\model\Trade;
use app\model\User;
use App\model\UserResLog;
use support\Common;
use support\Constant;

class TradeService
{
    public function checkIsOff()
    {
        //获取所有交易数据
        Trade::query()->where('is_off', 0)->orderBy('id')->chunkById(100, function ($trade_lists) {
            foreach ($trade_lists as $model) {
                $now_time = time();
                $expire_time = strtotime($model->created_at) + $model->on_time * 86400;
                if ($now_time >= $expire_time) {
                    //发送邮件给出售者
                    $mail_model = new Mail();
                    $mail_model->type = Constant::MAIL_TYPE_4;
                    $mail_model->uuid = $model['uuid'];
                    $mail_model->role_id = $model['role_id'];
                    $mail_model->title = '交易行物品下架';
                    $mail_model->content = sprintf("物品%s已下架，请及时领取", $model['item_name']);
                    $attach_items = [];
                    $attach_items[] = ['item_key' => $model['item_key'], 'num' => $model['num'], 'attrs' =>
                        $model['attrs']];
                    $mail_model->attach = $attach_items;
                    $mail_model->save();

                    $model->is_off = 1;
                    $model->save();

                }

            }
        });
    }

    public
    function sendLotteryEmail()
    {
        //查询昨天登录的所有玩家
        $start_time = mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        $end_time = mktime(23, 59, 59, date("m"), date("d") - 1, date("Y"));

        $users = UserResLog::query()->select('uuid', 'role_id')->whereBetween('created_at', [$start_time, $end_time])->groupBy('uuid', 'role_id')->get();
        $users = $users ? $users->toArray() : [];
        // print_r($users);

        $temp_arr = [];
        foreach ($users as $user) {
            $temp_arr[$user['uuid'] . '-' . $user['role_id']] = 1;
        }

        //获取已经抽到的
        $mails = Mail::query()->where('type', Constant::MAIL_TYPE_5)->get();
        $mails = $mails ? $mails->toArray() : [];
        $has_get_uids = [];
        foreach ($mails as $item) {
            $has_get_uids[$item['uuid']] = 1;
        }

        $rand_keys = [];
        $count = 0;
        for ($i = 0; $i < 1000; $i++) {
            $rand_key = array_rand($temp_arr);
            if (empty($has_get_uids[$rand_key])) {
                $rand_keys[$rand_key] = 1;
                $count = $count + 1;
            }
            if ($count >= 3) {
                break;
            }
        }

        // print_r($rand_keys);
        // die;

        //发邮件
        foreach ($rand_keys as $rand_key => $val) {
            // 发送邮件
            list($uuid, $role_id) = explode('-', $rand_key);

            $mail_model = new Mail();
            $mail_model->type = Constant::MAIL_TYPE_5;
            $mail_model->uuid = $uuid;
            $mail_model->role_id = $role_id;
            $mail_model->title = '内测幸运玩家';
            $mail_model->content = '尊敬的内测玩家您好，感谢您对游戏的支持与厚爱，恭喜您成为幸运玩家，获得中秋限定神翼，此奖励将于2024年9月17日中秋节当天10点统一发放。';
            $mail_model->save();
        }


    }

}